package mediatheque.model;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public abstract class DocumentModel extends AbstractModel{
	
	private static final long serialVersionUID = -4127803683079589268L;
	protected String type;
	protected String nom;
	protected String identifiant;
	protected String etat;
	protected String dateAjout;
	protected String description;
	protected boolean empruntable;
	protected ArrayList<String> listeTheme;
	
	private static int NOMBRE_DOCUMENTS = 0;
	
	public DocumentModel() {
		this("N/A","N/A","N/A","N/A",true,null);
	}
	
	public DocumentModel(String type, String nom,String etat,  
						String description, boolean empruntable, ArrayList<String> listeTheme){
		
		this.type = type;
		this.nom = nom;
		this.etat = etat;
		
		Calendar c1 = Calendar.getInstance();
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");		
		this.dateAjout = dateFormat.format(c1.getTime());
		
		this.description = description;
		this.empruntable = empruntable;
		this.listeTheme = listeTheme;
		
		NOMBRE_DOCUMENTS++;
	}

	public String getDateAjout() {
		return dateAjout;
	}
	
	public String getDescription() {
		return description;
	}
	
	public String getEtat() {
		return etat;
	}
	
	public String getIdentifiant() {
		return identifiant;
	}
	
	public ArrayList<String> getListeTheme() {
		return listeTheme;
	}
	
	public String getNom() {
		return nom;
	}
	
	public String getType() {
		return type;
	}
	
	public boolean isEmpruntable() {
		return empruntable;
	}
	
	public void setDateAjout(String dateAjout) {
		this.dateAjout = dateAjout;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public void setEmpruntable(boolean empruntable) {
		this.empruntable = empruntable;
	}
	
	public void setEtat(String etat) {
		this.etat = etat;
	}
	
	public void setIdentifiant(String identifiant) {
		this.identifiant = identifiant;
	}
	
	public void setListeTheme(ArrayList<String> listeTheme) {
		this.listeTheme = listeTheme;
	}
	
	public void setNom(String nom) {
		this.nom = nom;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	


	private void writeObject(ObjectOutputStream oos) throws IOException {
		oos.defaultWriteObject();
		oos.writeObject(new Integer(NOMBRE_DOCUMENTS));
	}

	private void readObject(ObjectInputStream ois)
			throws ClassNotFoundException, IOException {
		ois.defaultReadObject();
		NOMBRE_DOCUMENTS = (Integer) ois.readObject();
	}

	public boolean ressemble(String id, String nom, String desc, String type,
			String date) {
		return (getIdentifiant().toLowerCase().contains(id.toLowerCase()) &&
				getNom().toLowerCase().contains(nom.toLowerCase()) &&
				getDescription().toLowerCase().contains(desc.toLowerCase()) &&
				getType().toLowerCase().contains(type.toLowerCase()) &&
				getDateAjout().toLowerCase().contains(date.toLowerCase()));
		
	}

	public boolean ressemble(String emp) {
		return (getIdentifiant().toLowerCase().contains(emp.toLowerCase()) ||
				getNom().toLowerCase().contains(emp.toLowerCase()) ||
				getDescription().toLowerCase().contains(emp.toLowerCase()));
	}
	

}
